/**
 * @author jingy E-mail: jingyu23@yahoo.cn
 *
 * @version 1.0 创建时间：2011-11-24 下午03:04:38
 */
package com.youth.custom.tag;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class PageInfo implements Serializable {
	private static final long serialVersionUID = -5946267264228382136L;
	private int pageSize = 20;
	private int currentPage = 1;
	private int totalCount = 0;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getCurrentCount() {
		return (this.currentPage - 1) * this.pageSize;
	}

	public int getTotalPage() {
		return (this.totalCount + this.pageSize - 1) / this.pageSize;
	}

	/**
	 * 拼写查询条件，并将查询条件放入request.attribute中,如果传入参数为集合，则attribute中的值也为集合
	 * 
	 * @param req
	 * @return
	 */
	public static String getCondition(HttpServletRequest req) {
		StringBuffer sb = new StringBuffer();
		Enumeration<String> names = req.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			if (name.equals("pageSize") || name.equals("currentPage")) {
				continue;
			}
			String[] values = req.getParameterValues(name);
			if (null != values && values.length > 1) {
				List<String> attrs = new ArrayList<String>();
				for (int i = 0; i < values.length; i++) {
					String temp = values[i];
					temp = null == temp || "null".equals(temp.trim()) || "".equals(temp.trim()) ? null : temp.trim();
					sb.append(name + "/" + temp + "/");
					attrs.add(temp);
				}
				req.setAttribute(name, attrs);
			} else if (null != values && values.length == 1) {
				sb.append(name + "/" + values[0] + "/");
				req.setAttribute(name, values[0]);
			}
		}
		if (sb.length() > 0) {
			return sb.toString().substring(0, sb.length() - 1);
		} else {
			return "";
		}
	}
}
